var Index = {
		id: "indexTable",
	    table: null,
	    layerIndex: -1,
	    modelPath: "/templateHtml"
}

/**
 * 初始化表格列
 */
Index.initColumn = function(){
	var statusFormat = function(value, row, index){
		if(row.tempStatus == 1){
			return "<span class='label label-primary'>发布</span>";
		}else if(row.tempStatus == -1){
			return "<span class='label label-danger'>无效</span>";
		}else if(row.tempStatus == 0){
			return "<span class='label label-warning'>草稿</span>";
		}
	};
	var tempCodeFormat = function(value, row, index){
//		return "<a href='"+row.fastUrl+"' target='_blank'>"+value+"</a>";
		return "<a href='"+Index.modelPath+"/view/"+row.id+"' target='_blank'>"+value+"</a>";
	};
	var opFormat = function(value, row, index){
		var op = [];
		if(row.tempStatus == 1 && row.maxVersion==row.tempVersion){
			op.push("<a title='变更' class='btn btn-primary btn-xs op-modify'><i class='fa fa-fw fa-google-plus'></i> </a>&nbsp;");
		}else if(row.tempStatus == 0 && row.maxVersion==row.tempVersion){
			op.push("<a title='编辑' class='btn btn-success btn-xs op-edit'><i class='fa fa-fw fa-edit'></i> </a>&nbsp;");
			op.push("<a title='删除' class='btn btn-danger btn-xs op-delete'><i class='fa fa-fw fa-trash'></i> </a>");
		}
		return op.join("");
	};
	var opEvents = {
			'click .op-edit': function (e, value, row, index) {
				Index.edit(row.id);
				return false;
			},
			'click .op-modify': function (e, value, row, index) {
				Index.modify(row.id);
				return false;
			},
			'click .op-delete': function (e, value, row, index) {
				Index.delete(row.id);
				return false;
			}
	};
	return [
//		   {field: 'selectItem', checkbox: true,formatter: checkboxFormat},
	       {title: '模板编号', field: 'tempCode', align: 'center', valign: 'middle', formatter: tempCodeFormat},
	       {title: '模板名称', field: 'tempName', align: 'center', valign: 'middle'},
	       {title: '模板类型', field: 'tempType', align: 'center', valign: 'middle'},
	       {title: '模板版本', field: 'tempVersion', align: 'center', valign: 'middle'},
	       {title: '模板状态', field: 'statusDesc', align: 'center', valign: 'middle', formatter: statusFormat},
	       {title: 'tempStatus', field: 'tempStatus', visible: false},
	       {title: 'id', field: 'id', visible: false},
	       {title: 'fastUrl', field: 'fastUrl', visible: false},
	       {title: 'maxVersion', field: 'maxVersion', visible: false},
	       {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle'},
	       {title: '操作', field: '', align: 'center', valign: 'middle', formatter: opFormat, events: opEvents}
	];
}

/**
 * 查询
 */
Index.search = function (pageNo) {
	var queryData = {};
	$(".form-control").each(function(i,v){ 
		queryData[v.id] = $("#"+v.id).val();
    })
    pageNo = pageNo == undefined ? 1 : pageNo;
    Index.table.refresh({ pageNumber:pageNo,  query: queryData});
//    $('html,body').animate({scrollTop:0},'fast');
};
Index.reset = function () {
	$(".form-control").each(function(i,v){ 
		$("#"+v.id).val("");
    })
    Index.search();
};
Index.edit = function(id){
	var index = layer.open({
		type: 2,
		title: '新建/编辑模板',
		maxmin: false,
		move: false,
		area: ['100%', '100%'],
		content: Base.ctxPath + Index.modelPath+'/edit/'+id
	});
	this.layerIndex = index;
}
Index.modify = function(id){
	var index = layer.open({
		type: 2,
		title: '变更模板',
		maxmin: false,
		move: false,
		area: ['100%', '100%'],
		content: Base.ctxPath + Index.modelPath+'/modify/'+id
	});
	this.layerIndex = index;
}
Index.delete = function (id) {
	Tip.confirm("确认是否删除？", function(){
		var ajax = new $ax(Base.ctxPath + Index.modelPath+ "/delete", function(data){
			Tip.success("操作成功!");
			Index.table.refresh();
		},function(data){
			Tip.error(data.responseJSON.message);
		});
		ajax.set({id:id});
		ajax.start();
	});
};
Index.editStatus = function (id,status) {
	Tip.confirm("确认更新状态？", function(){
		var ajax = new $ax(Base.ctxPath + Index.modelPath+ "/save", function(data){
			Tip.success("操作成功!");
			Index.table.refresh();
		},function(data){
			Tip.error(data.responseJSON.message);
		});
		ajax.set({id:id,status:status});
		ajax.start();
	});
};
$(function(){
	//表格初始化
	var defaultColunms = Index.initColumn();
    var table = new BSTable(Index.id, Base.ctxPath +Index.modelPath+ "/list", defaultColunms);
    table.setPaginationType("server");
    Index.table = table.init();
    $("#" + Index.id).on("page-change.bs.table", function (e, number, size) {
    	Index.search(number);
    });
    
    $("#searchBtn").on("click", function(){
    	Index.search();
    });
    $("#resetBtn").on("click", function(){
    	Index.reset();
    });
    $("#addBtn").on("click", function(){
    	Index.edit(0);
    });
})